﻿using HC.Standard.UniversalAudioScheme;
using HC.Standard.UniversalAudioScheme.Models;
using InnerAudioScheme.Models;
using Microsoft.Win32;
using System.Data;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.IO;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using InnerAudioScheme.PopupWindows;

namespace InnerAudioScheme
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DataTable? _dataTable = null;
        private AudioCombination? _audioCombination = null;
        private AudioCombination _parentScheme = new AudioCombination();
    


        List<string> listFileNames = new List<string>();


        public ObservableCollection<string> ListFileNames { get; set; }

       public static string  selectedPath = string.Empty;

        // json转出的所有音频
        private static List<AudioInfo> _allAudio = new List<AudioInfo>();

        public MainWindow()
        {
            InitializeComponent();
            App.Logger.Info("what isnew ");

            if (ListFileNames==null)
            {
                ListFileNames = new ObservableCollection<string>();
            }
        }

        private void treeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void AddBrotherScheme_Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void AddSchemeRelation_Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void CreateSolutionFile_Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void GetAudioIndexTable_Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void AddSonScheme_Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ImportSolutionFile_Button_Click(object sender, RoutedEventArgs e)
        {
            string filePath = "";
            string textContent =System.IO.File.ReadAllText(filePath);
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

        }

        private void openProjectDir_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowDialog();

            if (folderBrowserDialog.SelectedPath ==string.Empty)
            {

                return;
            }
            else
            {
                Console.WriteLine(folderBrowserDialog.SelectedPath.Trim());

                // string fullPath = Path.GetFullPath(Path.Combine(currentDirectory, relativePath));
                string fullPath = folderBrowserDialog.SelectedPath.Trim();
                selectedPath=fullPath; 

                projectBlock.Text = "当前项目： " + fullPath.Split('\\').Last();

                 string[] dir = Directory.GetFiles(fullPath);
                string text = string.Empty;
                byte[] mybyte = null;



                // List<string> listFileNames = new List<string>();
                if (ListFileNames.Count > 0)
                { 
                    listFileNames.Clear();
                    ListFileNames.Clear();
                         }

                for (int i = 0; i < dir.Length; i++)
                {
                    if (Path.GetFileNameWithoutExtension(dir[i]).StartsWith("strAudioJson"))
                    {
                        string strAudioJsonText =  GetstrAudioJsonTxt(dir[i]);
                        if (!string.IsNullOrEmpty(strAudioJsonText))
                        {
                            _allAudio = JsonConvert.DeserializeObject<List<AudioInfo>>(strAudioJsonText);
                          //  solutionPreviousBlk.Text = strAudioJsonText;
                        }
                    }
                    else
                    {

                        string strTempAudioJsonText = GetstrAudioJsonTxt(dir[i]);

                        var audio = JsonConvert.DeserializeObject<AudioCombination>(strTempAudioJsonText);
                        //  listFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                        //  ListFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                        ListFileNames.Add(audio?.name);
                    }


                   
                }

                if (ListFileNames.Count == 0)
                {
                    projectBlock.Text = "当前项目： - " + fullPath.Split('\\').Last();

                    solutionsList.ItemsSource = null;
                    solutionPreviousBlk.Text = string.Empty;

                }
                else
                {
                    // solutionsList.ItemsSource = listFileNames;
                    solutionsList.ItemsSource = ListFileNames;
                }


              

            }


        
        }


        public string GetstrAudioJsonTxt( string filePath)
        {
           // string filePath = "Resources\\strAudioJson.txt";

            if (File.Exists(filePath))
            {
                string text = File.ReadAllText(filePath);
                byte[] mybyte = Encoding.UTF8.GetBytes(text);
                return Encoding.UTF8.GetString(mybyte);
            }
            else
            {
                return null;
            }
        }


        private void solutionsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.solutionsList.SelectedItem == null) {
                return;
            }
            string selectFileName=this.solutionsList.SelectedItem.ToString();


            string[] dir = Directory.GetFiles(selectedPath);           

            for (int i = 0; i < dir.Length; i++)
            {
                if (Path.GetFileNameWithoutExtension(dir[i]).StartsWith("strAudioJson"))
                {
                                    }
                else
                {

                    string strTempAudioJsonText = GetstrAudioJsonTxt(dir[i]);

                    var audio = JsonConvert.DeserializeObject<AudioCombination>(strTempAudioJsonText);
                    //  listFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                    //  ListFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                    if (audio.name== selectFileName)
                    {
                        solutionPreviousBlk.Text = strTempAudioJsonText;
                        break;
                    }
                   // ListFileNames.Add(audio?.name);
                }
            }


        
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ConfigWindow config = new ConfigWindow();
            config.strFileContent = string.Empty;
            config._allAudio = _allAudio;

            config.Show();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            string selectFileName = this.solutionsList.SelectedItem?.ToString();

            if (this.solutionsList.SelectedItem==null) 
            {
                System.Windows.MessageBox.Show("请先选择要编辑的方案！");
                return;

            }

            string strAudioJsonText = GetstrAudioJsonTxt(selectedPath + "\\" + selectFileName + ".txt");

            string[] dir = Directory.GetFiles(selectedPath);

            for (int i = 0; i < dir.Length; i++)
            {
                if (Path.GetFileNameWithoutExtension(dir[i]).StartsWith("strAudioJson"))
                {
                }
                else
                {

                    string strTempAudioJsonText = GetstrAudioJsonTxt(dir[i]);

                    var audio = JsonConvert.DeserializeObject<AudioCombination>(strTempAudioJsonText);
                    //  listFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                    //  ListFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                    if (audio.name == selectFileName)
                    {
                        //  solutionPreviousBlk.Text = strTempAudioJsonText;

                        ConfigWindow config = new ConfigWindow();
                        config.strFileContent = strTempAudioJsonText;
                        config._allAudio = _allAudio;
                        config.Show();

                        break;
                    }
                    // ListFileNames.Add(audio?.name);
                }
            }



          
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            string selectFileName = this.solutionsList.SelectedItem?.ToString();
            if (string.IsNullOrEmpty(selectFileName))
            {
                
                this.btnDelete.IsEnabled = false;
                return;

            }
            

            if (System.Windows.MessageBox.Show("确任删除方案?",
                    "删除文件",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                // Do something here


                string[] dir = Directory.GetFiles(selectedPath);

                for (int i = 0; i < dir.Length; i++)
                {
                    if (Path.GetFileNameWithoutExtension(dir[i]).StartsWith("strAudioJson"))
                    {

                    }
                    else
                    {

                        string strTempAudioJsonText = GetstrAudioJsonTxt(dir[i]);

                        var audio = JsonConvert.DeserializeObject<AudioCombination>(strTempAudioJsonText);
                        //  listFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                        //  ListFileNames.Add(Path.GetFileNameWithoutExtension(dir[i]));
                        if (audio.name == selectFileName)
                        {
                          //  solutionPreviousBlk.Text = strTempAudioJsonText;

                            ListFileNames.Remove(selectFileName);
                            File.Delete(dir[i]);
                            break;
                        }
                        // ListFileNames.Add(audio?.name);
                    }
                }



             //   ListFileNames.Remove(selectFileName);

             //    File.Delete(selectedPath+@"\"+selectFileName+".txt");
                // ListFileNames.Remove();

            }
            else
            { 
            }
        }
    }
}